package com.cloudptt.api.models.call;



import java.util.HashMap;
import java.util.Map;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.log.Log;
import com.mcptt.db.model.AddrInfo;

import android.os.Parcel;
import android.os.Parcelable;

import org.json.JSONObject;

public class RtcpMsg  implements Parcelable
{
	public final static String Title_FLOOR_IDLE = "FLOOR_IDLE";
	public final static String Title_FLOOR_REVOKE = "FLOOR_REVOKE";
	public final static String Title_FLOOR_GRANTED = "FLOOR_GRANTED";
	public final static String Title_FLOOR_TAKEN = "FLOOR_TAKEN";
	public final static String Title_FLOOR_DENY = "FLOOR_DENY";
	public final static String Title_FLOOR_QUEUEINFO = "FLOOR_QUEUEINFO";
	
	public final static String Title_Video_Key = "MCV";
	
	public final static String Title_MCV1_GRANTED = "MCV1_TRANSM_GRANTED";
	public final static String Title_MCV1_REJECTED = "MCV1_TRANSM_REJECTED";
	public final static String Title_MCV1_REVOKED = "MCV1_TRANSM_REVOKED";
	public final static String Title_MCV1_TRANSM_MEDIA_NOTE = "MCV1_TRANSM_MEDIA_NOTE";
	public final static String Title_MCV1_TRANSM_RECV_MEDIA_RSP = "MCV1_TRANSM_RECV_MEDIA_RSP";
	public final static String Title_MCV1_TRANSM_RECEP_NOTE = "MCV1_TRANSM_RECEP_NOTE";
	public final static String Title_MCV1_TRANSM_MEDIA_END_NOTE = "MCV1_TRANSM_MEDIA_END_NOTE";
	public final static String Title_MCV1_IDLE = "MCV1_TRANSM_IDLE";
	
	public final static String Title_MCV2_RANSM_MEDIA_END_REQ = "MCV2_TRANSM_MEDIA_END_REQ";
	public final static String Title_MCV2_TRANSM_MEDIA_END_RSP = "MCV2_TRANSM_MEDIA_END_RSP";
	public final static String Title_MCV2_TRANSM_RECEP_END_REQ = "MCV2_TRANSM_RECEP_END_REQ";
	public final static String Title_MCV2_TRANSM_RECEP_END_RSP = "MCV2_TRANSM_RECEP_END_RSP";
	public final static String Title_MCV2_TRANSM_ACK_CONTROL = "MCV2_TRANSM_ACK_CONTROL";
	public final static String Title_MCV2_TRANSM_MEDIA_RECEP_END_NOTE = "MCVX_TRANSM_MEDIA_REMOTE_END_RECT_NOTIFY";
	
	public final static String Key_CallKey = "key";
	public final static String Key_McpttId = "mcpttId";
	public final static String Key_Cause = "cause";
	public final static String Key_Duration = "duration";
	public final static String Key_UserId = "userid";
	public final static String Key_VideoSsrc = "videoSsrc";
	public final static String Key_AudioSsrc = "audioSsrc";


	
	private final static int Duration_Default = 30;
	private final static int Cause_Default = 0;
	
	
//	public final static int Title_ERROR_ID = -1;
//	public final static int Title_IDLE_ID = 0;
//	public final static int Title_REVOKE_ID = 1;
//	public final static int Title_GRANTED_ID = 2;
//	public final static int Title_TAKEN_ID = 3;
//	public final static int Title_DENY_ID = 4;
	
//	public final static String Title_TAKEN = "FLOOR_TAKEN";
	private String extStr = null;
	private String title = null;
	private Map<String,String> valueMap = null;
//	private long callId = 0;
//	
////	private String THIS_FILE = "ExtStrInfo";
//
//	public long getCallId() {
//		return callId;
//	}
//
//	public void setCallId(long callId) {
//		this.callId = callId;
//	}

	public RtcpMsg(String extStr)
	{
		this.extStr = extStr;
		parse();
	}
	
	public void parse()
	{
		String[] strArr = extStr.split(";");
		valueMap = new HashMap<String,String>();
		for(int i = 0; i < strArr.length; i++)
		{
			String value = strArr[i];
			if(!value.contains("="))
			{
				this.title = strArr[i].trim();
			}
			else
			{
				String[] valueArr = value.split("=");
				if(valueArr.length == 2)
				{
					if(valueArr[0] != null
							&& valueArr[1] != null)
					{
						String key = valueArr[0].trim();
						String val = valueArr[1].trim();
						Log.debug(RtcpMsg.class, "key = " + key + " val = " + val);
//						if(key.equals(Key_McpttId))
//						{
//							val =  AddrInfo.parseUserFromTakenStr(val);
//						}
						valueMap.put(key, val);
					}
				}
			}
		}
	}

	public String getTitle() {
		return title;
	}

	public boolean isVideoTransRtcpMsg()
	{
		if(title != null
				&& (title.equals(Title_MCV1_GRANTED)
				|| title.equals(Title_MCV1_REJECTED)
				|| title.equals(Title_MCV1_REVOKED)
				|| title.equals(Title_MCV1_TRANSM_MEDIA_NOTE)
				|| title.equals(Title_MCV1_TRANSM_MEDIA_END_NOTE)
				|| title.equals(Title_MCV1_IDLE)
				|| title.equals(Title_MCV2_RANSM_MEDIA_END_REQ)
				|| title.equals(Title_MCV2_TRANSM_MEDIA_END_RSP)
				|| title.equals(Title_MCV2_TRANSM_MEDIA_RECEP_END_NOTE)))
		{
			return true;
		}
		return false;
	}

	public boolean isVideoRecvRtcpMsg()
	{
		if(title != null
				&& (title.equals(Title_MCV1_TRANSM_RECV_MEDIA_RSP)
				|| title.equals(Title_MCV1_TRANSM_RECEP_NOTE)
				|| title.equals(Title_MCV2_TRANSM_RECEP_END_REQ)
				|| title.equals(Title_MCV2_TRANSM_RECEP_END_RSP)
				|| title.equals(Title_MCV1_TRANSM_MEDIA_END_NOTE)))
		{
			return true;
		}
		return false;
	}
	public boolean isVideoRtcpMsg()
	{
		if(title != null
				&& title.startsWith(Title_Video_Key))
		{
			return true;
		}
		return false;
	}

	public String getValue(String key)
	{
		if(key == null
				|| key.equals(""))
		{
			return "";
		}
		
		String value = valueMap.get(key);
		if( value != null ) {
			value = value.trim();
			return value;
		}
		
		return "";
	}

	public String getTakenNum()
	{
		Log.debug(RtcpMsg.class, "getTakenNum valueMap = " + valueMap);
		if(this.getValue(RtcpMsg.Key_McpttId) != null)
		{
			String val = this.getValue(RtcpMsg.Key_McpttId);
			Log.debug(RtcpMsg.class, "getTakenNum val = " + val);
			if(val.contains("@")) {
				return AddrInfo.toNumFromSipUrl(val);
			}
			else {
				return val;
			}
		}
		return "";
	}

	public String getTransInfoJson()
	{
		try {
			JSONObject jsonObject = new JSONObject();
			String val = this.getValue(Key_UserId);
			jsonObject.put(Key_UserId,val);

			val = this.getValue(Key_VideoSsrc);
			long ssrc = CheckData.parseLong(val);
			jsonObject.put(Key_VideoSsrc,ssrc);

			val = this.getValue(Key_AudioSsrc);
			ssrc = CheckData.parseLong(val);
			jsonObject.put(Key_AudioSsrc,ssrc);

			return jsonObject.toString();
		}catch (Exception e)
		{
			e.printStackTrace();
		}
		return "";
	}

	public String getVideoTakenNum()
	{
		Log.debug(RtcpMsg.class, "getTakenNum valueMap = " + valueMap);
		if(this.getValue(RtcpMsg.Key_UserId) != null)
		{
			String val = this.getValue(RtcpMsg.Key_UserId);
			Log.debug(RtcpMsg.class, "getTakenNum val = " + val);
			if(val.contains("@")) {
				return AddrInfo.toNumFromSipUrl(val);
			}
			else {
				return val;
			}
		}
		return "";
	}
	
	public int getDuration()
	{
		String value = getValue(Key_Duration);
		if(value != null
				&& value.length() > 0)
		{
			return Integer.parseInt(value);
		}
		return Duration_Default;
	}
	
	public int getCause()
	{
		String value = getValue(Key_Cause);
		if(value != null
				&& value.length() > 0)
		{
			return Integer.parseInt(value);
		}
		return Cause_Default;
	}
	public String getExtStr() {
		return extStr;
	}
	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("RtcpMsg :");
		strBuf.append(title).append(":");
		strBuf.append(valueMap);
		strBuf.append(" isVideoRtcpMsg = ").append(isVideoRtcpMsg());
		
		return strBuf.toString();
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int flags) {
		// TODO Auto-generated method stub
		out.writeString(extStr);
		out.writeString(title);
		out.writeMap(valueMap);
	}

	public static final Parcelable.Creator<RtcpMsg> CREATOR = new Parcelable.Creator<RtcpMsg>() {
		public RtcpMsg createFromParcel(Parcel in) {
			return new RtcpMsg(in);
		}

		public RtcpMsg[] newArray(int size) {
			return new RtcpMsg[size];
		}
	};

	private RtcpMsg(Parcel in) {
		this.extStr = in.readString();
		this.title = in.readString();
		this.valueMap = in.readHashMap(HashMap.class.getClassLoader());  
	}
}


